// pages/main/index.ts
import { get } from '@/utils/http'
import { calendarTime, diffCalDay } from '@/utils/format'
import { areaList } from '@vant/area-data'
const app = getApp()

Page({
  data: {
    selRoom:'1',   //选择房型 钟点房/全日房
    api:app.globalData.api,
    isAddress:false,            //选取地点弹出层
    isCalendar:false,          //是否展示日历,
    areaList,
    areaCode: "",       //区级行政区码
    myPosition: {
      position:'',
      latitude: '',
      longitude: '',
    },
    inputVal:'',
    calendar:{
      startData:'',
      startWeek:'今日',
      endData:'',
      endWeek:'明日',
      diffTime: 1  //相差的几天
    },
    calendarParams:{
      start:0,
      end:0
    }
  },
  onSelRoom(el:any) { 
    this.setData({
      selRoom: el.target.dataset.show
    })
  },
  getMyPosition(){
    const that = this
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        const lat = res.latitude
        const long = res.longitude
        wx.request({
          url:'https://api.map.baidu.com/reverse_geocoding/v3/?ak=iQ1VbswaSirtDcHCUwoPGCYDNLwNidal&location=' + lat + ',' + long + '&output=json&radius=500',
          method:'GET',
          success(ress) {
            that.setData({
              myPosition:{
                position:ress.data.result.formatted_address,
                longitude:long,
                latitude:lat
              },
              areaCode: "",
            })
          }
        })
      }
    })
  },
  onShowCal() {
    wx.hideTabBar({ animation:true })
    this.setData({ isCalendar:true })
  },
  //选择的日期
  onSelCal(event) {
    wx.showTabBar({ animation:true })
    const [ start, end ] = event.detail
    let startTime = calendarTime(start)
    let endTime = calendarTime(end)
    this.setData({ 
      isCalendar:false,
      calendar:{ 
        startData:startTime.time,
        startWeek:startTime.week,
        endData:endTime.time,
        endWeek:endTime.week,
        diffTime: diffCalDay(start,end)
      },
      calendarParams:{
        start: new Date(start).getTime(),
        end: new Date(end).getTime()
      }
    }) 
  },
  query() {
    const obj = {
      type: this.data.selRoom === '4'?'':this.data.selRoom,
      lon: this.data.myPosition.longitude,
      lat: this.data.myPosition.latitude,
      hotel_name: this.data.inputVal,
      city_id: this.data.areaCode
    }
    wx.navigateTo({
      url:'/layout/main/hotel/index?obj=' + JSON.stringify(obj) + '&calendar=' + JSON.stringify(this.data.calendarParams),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   *页面初始化进入主页 设置日期时间
   */
  onLoad() {
    const time = new Date().getTime()
    const today = time + 86400000
    let startTime = calendarTime(time)
    let endTime = calendarTime(today)
      this.setData({
        calendar:{
          startData:startTime.time,
          startWeek:startTime.week,
          endData:endTime.time,
          endWeek:endTime.week,
          diffTime:1
        },
        calendarParams:{
          start: time,
          end: today
        }
      })
  },
  selAddress(){
    this.setData({ isAddress:true })
  },
  onClose() {
    this.setData({ isCalendar:false })
  },
  onCloseAdd(){
    this.setData({ isAddress: false })
  },
  onSelAdd(val){
    let addressArr = val.detail.values
    let name = ''
    addressArr.forEach(el => {
      name += el.name
    })
    this.setData({ 
      isAddress: false,
      areaCode: addressArr[addressArr.length - 1].code,
      myPosition:{
        position: name,
        longitude:'',
        latitude:''
      }
    })
  }
})